<template>
  <el-row>
    <el-col :span="12">
      <DictType @on-select="selectDictType"></DictType>
    </el-col>
    <el-col :span="12">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>字典</span>
          <el-button style="float: right;" type="primary" size="mini" @click="saveDictVisible=true">新增</el-button>
        </div>
        <el-table highlight-current-row :data="data.records">
          <el-table-column prop="code" label="编码" width="150"> </el-table-column>
          <el-table-column prop="value" label="值" min-width="150" width="auto"> </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            width="150">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="showUpdate(scope.row.code)">编辑</el-button>
              <el-popconfirm title="确定要删除吗？" @onConfirm="remove(scope.row.code)">
                <el-button style="margin-left: 10px;" type="danger" size="mini" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="$config.defaultPageSizeOpts"
          :page-size="size"
          layout="total, sizes, prev, pager, next"
          :total="data.total">
        </el-pagination>
      </el-card>

      <SaveDict :visible="saveDictVisible"
                :typeCode="typeCode"
                :typeName="typeName"
                @on-close="saveDictVisible=false"
                @on-success="saveSuccess">
      </SaveDict>
      <UpdateDict :visible="updateDictVisible"
                  :typeCode="typeCode"
                  :typeName="typeName"
                  :code="code"
                  @on-close="updateDictVisible=false"
                  @on-success="updateSuccess">
      </UpdateDict>
    </el-col>
  </el-row>
</template>
<script>

import DictType from '@/views/system/dict/dict-type/DictType'
import { mapActions } from 'vuex'
import SaveDict from '@/views/system/dict/SaveDict'
import UpdateDict from '@/views/system/dict/UpdateDict'
export default {
  name: 'Dict',
  components: { UpdateDict, SaveDict, DictType },
  data() {
    return {
      data: {},
      params: {},
      form: {},
      current: 1,
      size: 10,
      saveDictVisible: false,
      updateDictVisible: false,
      code: 0,
      typeCode: '',
      typeName: ''
    }
  },
  methods: {
    selectDictType(val) {
      this.typeCode = val.code
      this.typeName = val.name
      this.search()
    },
    ...mapActions(['GetDictPage', 'RemoveDictById']),
    search() {
      this.params.current = this.current
      this.params.size = this.size
      this.loading = true
      this.GetDictPage({ typeCode: this.typeCode, params: this.params }).then(res => {
        this.loading = false
        if (res) {
          this.data = res
        }
      }).catch(() => {
        this.loading = false
      })
    },
    reset() {
      this.params = {}
      this.current = 1
    },
    searchByCondition() {
      this.current = 1
      this.search()
    },
    handleSizeChange(val) {
      this.size = val
      this.search()
    },
    handleCurrentChange(val) {
      this.current = val
      this.search()
    },
    saveSuccess() {
      this.search()
      this.saveDictVisible = false
    },
    showUpdate(code) {
      this.code = code
      this.updateDictVisible = true
    },
    updateSuccess() {
      this.search()
      this.updateDictVisible = false
    },
    remove(code) {
      this.RemoveDictById({ typeCode: this.typeCode, code }).then(() => {
        this.search()
      })
    }
  }
}
</script>
